// v2.0
#include "ISBNPrefix.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
// Constructor
ISBNPrefix::ISBNPrefix(const char* filename){
        fp=fopen(filename,"r");
}
ISBNPrefix::~ISBNPrefix(){
        if (fp!=NULL) fclose(fp);
}
ISBNPrefix::ISBNPrefix(const ISBNPrefix& source){
        fp=source.fp;
}
// Operator
ISBNPrefix& ISBNPrefix::operator=(const ISBNPrefix& source){
        this->fp=source.fp;
        return *this;
}
// 
int ISBNPrefix::registered(long area) const{
    long A,min,max;
     rewind(fp);
     while (!feof(fp)) {
           fscanf(fp,"%ld%ld%ld",&A,&min,&max);
           if (area==A) return true;
     }
     return false;
}
int ISBNPrefix::minNoDigits(long area) const{
    long A;
    char min[8];
    char max[7];
    rewind(fp);
    while (!feof(fp)) {
           fscanf(fp,"%ld%s%s",&A,min,max);
           if (area==A) return strlen(max);
     }
    return 1;
}
     
int ISBNPrefix::registered(long area, const char* publisher) const{
     long A;
     char min[8];
     char max[7];
     rewind(fp);
     while (!feof(fp)) {
           fscanf(fp,"%ld%s%s",&A,min,max);
           if (area==A && strlen(min)==strlen(publisher) )
           if (strcmp(publisher,min)>=0 && strcmp(publisher,max)<=0) return 1;
     }
     return 0;
}

